<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Tuvan_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	} 
	
	// Category                 
	function get_list($num=20,$offset=0)
	{
		//$this->db->where('nb_kind', $nb_kind) ;
		$this->db->where('bl_active !=',-1);
		$this->db->order_by('dt_create','desc');
		$query = $this->db->get('tuvan',$num,$offset);
		return $query->result();
	}
	
	function count_list()
	{
		$this->db->where('bl_active !=',-1);
		$query = $this->db->get('tuvan');
		return $query->num_rows();
	}
	
	function set_view($id)
	{
		$data = array(
			'is_read' => 1
		);
		$this->db->where('id',$id);
		$this->db->update('tuvan',$data) ;
	}
	
	function getId($id=0)
	{ 
		if(!empty($id)) $this->db->where('id',$id);
		$query = $this->db->get('tuvan');
		return $query->row();
	}
	
	function delete($id)
	{
		$data = array(
			'bl_active' 	=> -1
		);
		$this->db->where('id',$id);
		$this->db->update('tuvan',$data) ;
	}
	
	function remove()
	{
		$date = new DateTime();
		$date->modify('-3 month');
		$date_del = $date->format('Y-m-d H:i:s');
		
		$this->db->where('bl_active', -1);
		$this->db->where('dt_create <', $date_del);
		if($this->db->delete('tuvan')){
		  	return true;
		}else{
		  	return false;
		}
	}
	
	function save($tbl, $data, $name='', $id=0)
	{
		if($name)
		{
			$this->db->where($name, $id) ;
			if($this->db->update($tbl, $data))
				return $id ;
			else return false;
		}
		else
		{
			if($this->db->insert($tbl,$data))
				return $this->db->insert_id();
			else return false;
		}
	}
	/*function save($id,$noidung_vn,$noidung_en,$cusname)
	{
		$data=array(
			'lb_content_vn'=>$noidung_vn,
			'lb_content_en'=>$noidung_en,
			'lb_cusname'=>$cusname,
			'nb_kind'=>$id,
			'is_ans'=>1,
			'dt_create'=>date('Y-m-d H:i:s')
		);
		if($this->db->insert('tuvan',$data)) 
			return true;
		else 
			return false;
	}*/
	
	function update($id,$noidung_vn,$noidung_en,$cusname){
		$data=array(
			'lb_content_vn'=>$noidung_vn,
			'lb_content_en'=>$noidung_en,
			'lb_cusname'=>$cusname,
			'is_ans'=>1,
			'dt_create'=>date('Y-m-d H:i:s')
		);
		$this->db->where('id',$id);
		if($this->db->update('tuvan',$data)) return true;
		else return false;
	}
}
?>
